package org.dromara.nft.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.nft.domain.bo.NftItemBo;
import org.dromara.nft.domain.vo.NftItemVo;
import org.dromara.nft.domain.NftItem;
import org.dromara.nft.mapper.NftItemMapper;
import org.dromara.nft.service.INftItemService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * nftService业务层处理
 *
 * @author Lion Li
 * @date 2024-05-20
 */
@RequiredArgsConstructor
@Service
public class NftItemServiceImpl implements INftItemService {

    private final NftItemMapper baseMapper;

    /**
     * 查询nft
     *
     * @param id 主键
     * @return nft
     */
    @Override
    public NftItemVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询nft列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return nft分页列表
     */
    @Override
    public TableDataInfo<NftItemVo> queryPageList(NftItemBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NftItem> lqw = buildQueryWrapper(bo);
        Page<NftItemVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的nft列表
     *
     * @param bo 查询条件
     * @return nft列表
     */
    @Override
    public List<NftItemVo> queryList(NftItemBo bo) {
        LambdaQueryWrapper<NftItem> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NftItem> buildQueryWrapper(NftItemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NftItem> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getNftType()), NftItem::getNftType, bo.getNftType());
        lqw.likeRight(StringUtils.isNotBlank(bo.getTitle()), NftItem::getTitle, bo.getTitle());
        lqw.likeRight(bo.getPrice() != null, NftItem::getPrice, bo.getPrice());
        lqw.likeRight(StringUtils.isNotBlank(bo.getJd()), NftItem::getJd, bo.getJd());
        lqw.likeRight(StringUtils.isNotBlank(bo.getZbdj()), NftItem::getZbdj, bo.getZbdj());
        lqw.likeRight(StringUtils.isNotBlank(bo.getCbz()), NftItem::getCbz, bo.getCbz());
        lqw.likeRight(StringUtils.isNotBlank(bo.getHdnl()), NftItem::getHdnl, bo.getHdnl());
        lqw.likeRight(bo.getDwcbz() != null, NftItem::getDwcbz, bo.getDwcbz());
        lqw.likeRight(bo.getJccfz() != null, NftItem::getJccfz, bo.getJccfz());
        lqw.likeRight(bo.getJccfcb() != null, NftItem::getJccfcb, bo.getJccfcb());
        lqw.eq(bo.getItemId() != null, NftItem::getItemId, bo.getItemId());
        lqw.eq(StringUtils.isNotBlank(bo.getVersionNum()), NftItem::getVersionNum, bo.getVersionNum());
        return lqw;
    }

    /**
     * 新增nft
     *
     * @param bo nft
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(NftItemBo bo) {
        NftItem add = MapstructUtils.convert(bo, NftItem.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改nft
     *
     * @param bo nft
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(NftItemBo bo) {
        NftItem update = MapstructUtils.convert(bo, NftItem.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NftItem entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除nft信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
